package org.dromara.admin.tenant.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.admin.domain.bo.ContractBillBo;
import org.dromara.admin.domain.vo.ContractBillVo;
import org.dromara.admin.service.IContractBillService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户租金账单信息
 *
 * @author Lion Li
 * @date 2025-09-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/tenant/contractBill")
public class TenantContractBillController extends BaseController {

    private final IContractBillService contractBillService;

    /**
     * 查询租金账单信息列表
     */
    @GetMapping("/list")
    public TableDataInfo<ContractBillVo> list(ContractBillBo bo, PageQuery pageQuery) {
        return contractBillService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询我的租金账单信息列表
     */
    @GetMapping("/myList")
    public TableDataInfo<ContractBillVo> myList(ContractBillBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        return contractBillService.queryPageMyList(bo, pageQuery);
    }

    /**
     * 导出租金账单信息列表
     */
    @Log(title = "租金账单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ContractBillBo bo, HttpServletResponse response) {
        List<ContractBillVo> list = contractBillService.queryList(bo);
        ExcelUtil.exportExcel(list, "租金账单信息", ContractBillVo.class, response);
    }

    /**
     * 获取租金账单信息详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ContractBillVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(contractBillService.queryById(id));
    }

    /**
     * 新增租金账单信息
     */
    @Log(title = "租金账单信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ContractBillBo bo) {
        return toAjax(contractBillService.insertByBo(bo));
    }

    /**
     * 修改租金账单信息
     */
    @Log(title = "租金账单信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ContractBillBo bo) {
        return toAjax(contractBillService.updateByBo(bo));
    }

    /**
     * 判断用户未支付租金账单是否逾期
     */
    @GetMapping("/isOverdue")
    public R<Boolean> isOverdue() {
        Long userId = LoginHelper.getUserId();
        return R.ok(contractBillService.isOverdue(userId));
    }
}
